<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>
<body>
    <!-- 
        键盘事件：
            keydown  按下
            keyup    松开
            keypress 按下并松开
            先后顺序：keydown>keypress>keyup
            键盘事件对象:Event
                keyCode键盘码 which[兼容]
                key
                ctrlKey
                shiftKey
            alert("") 会中断后面代码执行  点了确定以后才会继续执行

     -->
    <textarea name="" id="" cols="30" rows="10"></textarea>
    <script>
        var text=document.querySelector("textarea")
        text.onkeydown=function(e)
        {
            

            var keycode=e.keyCode||e.which;
            //48~58为括号键数字的keyCode    96~106为右边数字的keyCode
            if(!((keycode>=48&&keycode<58)||(keycode>=96&&keycode<106)))

            {
                // return false;
            }

            console.log(keycode);
            // alert("keydown")
            console.log('onkeydown');
        }
        text.onkeyup=function(e)
        {
            this.value=parseInt(this.value)  //强制转化为数字
            // alert("keyup")
            console.log('onkeyup');
        }
        text.onkeypress=function(e)
        {
            // alert("keypress")
            console.log('onkeypress');

        }
    </script>
    
</body>
</html>